﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using ERDDiagram.ViewModel;
using System.Threading;
using System.Windows.Printing;
using System.Windows.Media.Imaging;
using System.IO;
using ImageTools;

namespace ERDDiagram
{
    [ScriptableType]
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Expander1.IsExpanded = true;
            this.MouseRightButtonDown += new MouseButtonEventHandler(MainPage_MouseRightButtonDown);
            pd = new PrintDocument();
            pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);
            
        }

        [ScriptableMember]
        public  void SaveDiagram(bool closeForm)
        {
            MainViewModel mymodel = (MainViewModel)this.DataContext;
            mymodel.CloseForm = closeForm;
            mymodel.SaveDiagram.Execute(null);
         
        }
        [ScriptableMember]
        public bool IsDirty()
        {
            MainViewModel mymodel = (MainViewModel)this.DataContext;
            return mymodel.IsDirty;

        }
        [ScriptableMember]
        public void RefreshNode(string entityname)
        {
            MainViewModel mymodel = (MainViewModel)this.DataContext;
            mymodel.RefreshNode(entityname);

        }
        private void MainPage_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
       
        PrintDocument pd; 
        private void printButton_Click(object sender, RoutedEventArgs e)
        {

            pd.Print("CRM 2011 ERD Designer");
        }
        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.PageVisual = LayoutRoot2;
        }

        private void page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            LayoutRoot.Width = page.ActualWidth;
            LayoutRoot.Height = page.ActualHeight;
            
        }

        private void ExportButton_Click(object sender, RoutedEventArgs e)
        {
            ExtendedImage extendedImage = LayoutRoot2.ToImage();

            
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Image Files (*.png)|*.png;";
            if (saveFileDialog.ShowDialog() == true)
            
            {
                using (Stream stream = saveFileDialog.OpenFile())
                {
                    // Use the write to stream extension method to write the image to the specified stream.
                    // The image encoder is selected by the extension of the name of the image.
                    extendedImage.WriteToStream(stream, saveFileDialog.SafeFileName);
                }
            }

        }

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.RowDefinitions[0].Height = new GridLength(130);
        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            LayoutRoot.RowDefinitions[0].Height = new GridLength( 40);
        }

      
    }
}
